package com.bub.pay.api.controller;

import com.bub.pay.api.service.SystemManagementService;
import com.bub.pay.core.entity.domain.UserLoginWhite;
import com.bub.pay.core.entity.vo.merchant.*;
import com.bub.pay.core.entity.vo.user.GoogleVo;
import com.bub.pay.core.entity.vo.user.PasswordVo;
import com.bub.pay.core.enums.DeleteFlagEnum;
import com.bub.pay.core.manager.email.EmailService;
import com.bub.pay.core.manager.loginWhite.UserLoginWhiteManager;
import com.bub.pay.core.manager.system.SysBankManager;
import com.bub.pay.core.manager.system.SysKvPairEntryManager;
import com.bub.pay.core.manager.user.UserCommonManager;
import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.base.Module;
import com.bub.pay.framework.common.entity.model.SessionModel;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.framework.common.util.ContextHolder;
import com.bub.pay.framework.wrap.annotation.SysLog;
import com.bub.pay.framework.wrap.annotation.SysLogType;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 商户端 , 系统管理
 *
 * @Author: wing
 * @Date: 2019/5/1 13:56
 */
@SysLogType("系统管理")
@RestController
@RequestMapping("/merchant/sys")
public class SystemManagementController extends BaseController {

    @Autowired
    private SystemManagementService service;

    @Resource
    private UserCommonManager userCommonManager;

    @Autowired
    private EmailService emailService;

    @Autowired
    private SysBankManager sysBankManager;
    @Autowired
    private UserLoginWhiteManager userLoginWhiteManager;
    @Autowired
    private SysKvPairEntryManager sysKvPairEntryManager;

    @RequiresPermissions("systemManage:accountInfo:view")
    @SysLog("查询账户信息")
    @PostMapping("/getUserAccount")
    public Object selectUserAccount(){
        return service.selectUserAccount();
    }


    @RequiresPermissions("systemManage:accountInfo:resetName")
    @SysLog("修改员工姓名")
    @PostMapping("/resetName")
    public Object resetNickName(@RequestBody @Valid ResetNickNameVo vo) throws Exception{
        return service.resetNickName(vo);
    }

    @RequiresPermissions("systemManage:accountInfo:resetPassword")
    @SysLog("修改登录密码")
    @PostMapping("/resetPassword")
    public Object changePassword(@RequestBody @Valid ResetPasswordVo vo) throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();

        //验证两次新登录密码是否一致
        if(!StringUtils.equals(vo.getNewPassword() , vo.getConfirmPassword())){
            throw new GlobalException(Module.merchant,Cause.twicePassword);
        }
        //验证就密码是否正确
        userCommonManager.checkLoginPassword(vo.getOldPassword(),Cause.error_password_old);

        PasswordVo reqVo = new PasswordVo();
        reqVo.setOldPassword(vo.getOldPassword());
        reqVo.setNewPassword(vo.getNewPassword());
        reqVo.setGoogleCode(vo.getGoogleCode());

        return userCommonManager.changePassword(sessionModel.getUserId().toString(), reqVo);
    }

    @RequiresPermissions("systemManage:accountInfo:resetEmail")
    @SysLog("发送邮箱验证码")
    @PostMapping("/sendEmailCode")
    public Object sendEmailCode(@RequestBody @Valid EmailCodeVo vo) throws Exception {
        emailService.sendEmailCode(vo.getEmail() , Module.merchant);
        return null;
    }

    @RequiresPermissions("systemManage:accountInfo:resetEmail")
    @SysLog("修改邮箱")
    @PostMapping("/resetEmail")
    public Object changeEmail(@RequestBody @Valid ResetEmailVo vo) throws Exception {

        //验证谷歌验证码
        userCommonManager.checkGoogle(vo.getGoogleCode());

        return service.changeEmail(vo);
    }

    @RequiresPermissions("systemManage:accountInfo:lookAccAddr")
    @SysLog("查看钱包地址")
    @PostMapping("/getAccAddr")
    public Object selectAccAddr(@RequestBody @Valid GoogleVo vo){
        userCommonManager.checkGoogle(vo.getGoogleCode());
        return service.selectAccAddr();
    }

    @RequiresPermissions("systemManage:loginWhiteList:add")
    @SysLog("添加绑定ip")
    @PostMapping("/addLoginIp")
    public Object addLoginIp(@RequestBody @Valid BindIpVo vo) throws Exception{
        //获取当前登录用户
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        UserLoginWhite white = new UserLoginWhite();
        white.setExtendId(sessionModel.getExtendId());
        white.setUserType(sessionModel.getUserType());
        white.setIp(vo.getIp());
        white.setRemarks(vo.getRemarks());
        white.setState(1);
        white.setCreateName(sessionModel.getNickName());
        white.setDelFlag(DeleteFlagEnum.using.flag);
        white.setCreateId(sessionModel.getUserId());
        white.setCreateDate(new Date());
        white.setUpdateId(sessionModel.getUserId());
        white.setUpdateDate(new Date());
        return userLoginWhiteManager.addLoginIp(Module.merchant,white);
    }

    @RequiresPermissions("systemManage:loginWhiteList:list")
    @SysLog("查询用户登录ip")
    @PostMapping("/getLoginIp")
    public Object queryLoginIp(@RequestBody QueryIpWhiteVO vo) throws Exception {

        return userLoginWhiteManager.queryLoginIp(vo);
    }

    @RequiresPermissions("systemManage:loginWhiteList:delete")
    @SysLog("删除用户登录ip,id:$(#map['id'])")
    @PostMapping("/delLoginIp")
    public Object delLoginIp(@RequestBody Map<String, Long> map){
        Long id = null;
        if (!map.containsKey("id") && map.get("id") == null){
           throw new GlobalException(Module.merchant, Cause.params_fail);
        }
        id = map.get("id");
        return userLoginWhiteManager.deleteUserLoginIp(id);
    }

    @RequiresPermissions("systemManage:systemLog:list")
    @SysLog("查询系统日志-第$(#vo.pageNo)页")
    @PostMapping("/queryLog")
    public Object selectSysLog(@RequestBody QueryIpWhiteVO vo){

        return service.querySysLog(vo);
    }

}
